package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：cui
 * @date ：Created in 2019/7/5
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("report")
public class ReportController {
    @Reference
    MemberService memberService;
    //查询会员报告
    @RequestMapping("getMemberReport")
    public Result getMemberReport(@RequestBody String[] monthlList){
       Map<String,Object> map = memberService.getMemberReport(monthlList);
       return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }
    //查询一年的会员报告
    @RequestMapping("getMemberReport4year")
    public Result getMemberReport4year(){
        Map<String,Object> map = memberService.getMemberReport4year();
        return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }
    //根据会员年龄绘制饼图
    @RequestMapping("findByMemberAge")
    public Result findByMemberAge(){
        try {
            List<Map<String,Object>> mapList=memberService.findByMemberAge();
            List<String> setmealNames=new ArrayList<>();
            Map<String,Object>resultMap=new HashMap<>();

            for (Map<String, Object> map : mapList) {
                Object name = map.get("name");
                setmealNames.add(String.valueOf(name));
            }
            resultMap.put("setmealNames",setmealNames);
            resultMap.put("setmealCount",mapList);
            return new Result(true,"查询成功",resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @RequestMapping("/getSexReport")
    public Result getSexReport() {
        try {

            List<Map<String, Object>> mapList = memberService.getSexRepot();

            Map<String, Object> resultMap = new HashMap<>();

            //查询到页面需要的数据 setmealCount,setmealNames
            List<String> setmealNAmes = new ArrayList<>();

            for (Map<String, Object> map : mapList) {
                Object name = map.get("name");
                String s = String.valueOf(name);
                setmealNAmes.add(s);
            }
            resultMap.put("setmealNames", setmealNAmes);
            resultMap.put("setmealCount", mapList);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
